#include "rtp/FeedbackPsAfb.h"
#include "utils.h"
#include "rtp/FeedbackPsRemb.h"
#include "rtp/Feedback.h"
namespace Rtp {
    namespace Rtcp {
        FeedbackPsAfbPacket* FeedbackPsAfbPacket::Parse(const uint8_t* data, size_t len) {
            if (len < Packet::CommonHeaderSize + FeedbackPacket::HeaderSize) {
                return nullptr;
            }

            auto* commonHeader = const_cast<CommonHeader*>(reinterpret_cast<const CommonHeader*>(data));
            std::unique_ptr<FeedbackPsAfbPacket> packet;
            constexpr size_t Offset = Packet::CommonHeaderSize + FeedbackPacket::HeaderSize;

            if (len >= Packet::CommonHeaderSize + FeedbackPacket::HeaderSize + 4 && Utils::Byte::Get4Bytes(data, offset) == FeedbackPsRembPacket::UniqueIdentifier) {
                packet.reset(FeedbackPsRembPacket::Parse(data, len));
            } else {
                packet.reset(new FeedbackPsAfbPacket(commonHeader));
            }
            return packet.release();
        }

        size_t FeedbackPsAfbPacket::Serialize(uint8_t *buffer) {
            size_t offset = FeedbackPsPacket::Serialize(buffer);
            std::memcpy(buffer + offset, this->data, this->size);
            return offset + this->size;
        }

        void FeedbackPsAfbPacket::Dump() const {
            FeedbackPsPacket::Dump();
        }
    }
}